package com.action;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MiaoshaAction {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient  redissonClient;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/")
    public String index()
    {
        return port;
    }

    @RequestMapping("/miao")
    @CrossOrigin("*")
    public String miao()
    {
        //加锁
        RLock  rLock = redissonClient.getLock("miaosha");

        //redisTemplate.boundValueOps("kuchun").set(5);
        //rLock.lock();
           Integer  kuchun = (Integer) redisTemplate.boundValueOps("kuchun").get();
           if (kuchun>0)
           {
               kuchun--;
               redisTemplate.boundValueOps("kuchun").set(kuchun);
           }
        //解锁
        //rLock.unlock();
        System.out.println(kuchun);

        return "success";
    }

}







